package cn.ncwenyou.game.area.service;

import cn.ncwenyou.game.area.vo.City;
import cn.ncwenyou.game.area.vo.District;
import cn.ncwenyou.game.area.vo.Province;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.io.IOUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class AreaService {

    private List<Province> provinces = new ArrayList<>();
    private final Map<String, List<City>> citiesByProvince = new HashMap<>();
    private final Map<String, List<District>> districtsByCity = new HashMap<>();

    @PostConstruct
    public void init() {
        loadProvinces();
        loadCities();
        loadDistricts();
    }

    public List<Province> getProvinces() {
        return provinces;
    }

    public List<City> getCitiesByProvince(String provinceId) {
        return citiesByProvince.getOrDefault(provinceId, new ArrayList<>());
    }

    public List<District> getDistrictsByCity(String cityId) {
        return districtsByCity.getOrDefault(cityId, new ArrayList<>());
    }

    private void loadProvinces() {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("province.json")) {
            if (inputStream != null) {
                String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                provinces = JSON.parseArray(content, Province.class);
            } else {
                System.err.println("省份数据不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException("加载省份数据失败", e);
        }
    }

    private void loadCities() {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("city.json")) {
            if (inputStream != null) {
                String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                JSONObject cityJsonObj = JSON.parseObject(content);
                for (String provinceId : cityJsonObj.keySet()) {
                    JSONArray cityArray = cityJsonObj.getJSONArray(provinceId);
                    List<City> cities = cityArray.toJavaList(City.class);
                    cities.forEach(city -> city.setProvinceId(provinceId));
                    citiesByProvince.put(provinceId, cities);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("加载城市数据失败", e);
        }
    }

    private void loadDistricts() {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("district.json")) {
            if (inputStream != null) {
                String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                JSONObject districtJsonObj = JSON.parseObject(content);
                for (String cityId : districtJsonObj.keySet()) {
                    JSONArray districtArray = districtJsonObj.getJSONArray(cityId);
                    List<District> districts = districtArray.toJavaList(District.class);
                    districts.forEach(district -> district.setCityId(cityId));
                    districtsByCity.put(cityId, districts);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("加载区县数据失败", e);
        }
    }

}
